
package jweslley.ProblemSetVolumes.VolumeVI;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

import jweslley.Problem;
import jweslley.Problem.Status;

/**
 * http://icpcres.ecs.baylor.edu/onlinejudge/external/6/673.html
 *
 * @author  Jonhnny Weslley
 * @version 1.00, 16/10/2008
 */
@Problem(Status.Accepted)
public class ParenthesesBalance {

	public static void main(final String[] args) throws IOException {
		StringBuilder out = new StringBuilder();
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.valueOf(in.readLine());
		for (int i = 0; i < n; i++) {
			out
			.append(check(in.readLine()) ? "Yes" : "No")
			.append('\n');
		}
		System.out.print(out);
	}

	static boolean check(String line) {
		if (line.trim().isEmpty()) {
			return true;
		}
		Stack<Character> stack = new Stack<Character>();
		char c;
		for (int i = 0; i < line.length(); i++) {
			c = line.charAt(i);
			if (c == '[' || c == '(') {
				stack.push(c);
			} else {
				if (stack.isEmpty()) {
					return false;
				}

				char top = stack.pop();
				if ((top == '(' && c != ')') || (top == '[' && c != ']')) {
					return false;
				}
			}
		}
		return stack.isEmpty();
	}

}
